/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.internal.sdk;

import static com.google.inject.Guice.createInjector;

import java.util.concurrent.ExecutionException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.inject.Injector;
import com.google.inject.Module;
 

/**
 * Activator class for this bundle.
 *
 * @author Thomas Kistel
 * @version $Revision: 760 $
 */
public class Activator extends AbstractUIPlugin {
    
    public static final String INJECTOR_KEY = "de.thwildau.tm.moses.ui.Sdk";

    private static Activator instance;
    
    private Cache<String, Injector> injectors = null;
    
    public Activator() {
        try {
            CacheLoader<String, Injector> loader = new CacheLoader<String, Injector>() {
                @Override
                public Injector load(String language) throws Exception {
                    Module uiModule = getUiModule(language);
                    return createInjector(uiModule);
                }
            };
            injectors = CacheBuilder.newBuilder().build(loader);
        } catch(LinkageError e) {
            logError("Could not init Injector. Incompatible Injector library.",  e);
        }
    }
    
    public static Activator getDefault() {
        return instance;
    }
    
    /**
     * Returns the Id (Bundle-SymbolicName) of this Bundle.
     *  
     * @return the Id (Bundle-SymbolicName) of this Bundle
     */
    public static String getBundleId() {
        return getDefault().getBundle().getSymbolicName();
    }
    
    public static void debugInfo(String message) {
        if(getDefault().isDebugging()) {
            getDefault().getLog().log(new Status(IStatus.INFO, getBundleId(), "DEBUG: " + message));
        }
    }
    
    public static void logInfo(String message) {
        getDefault().getLog().log(new Status(IStatus.INFO, getBundleId(), message));
    }
    
    public static void logWarning(String message) {
        getDefault().getLog().log(new Status(IStatus.WARNING, getBundleId(), message));
    }
    
    public static void logError(String message, Throwable t) {
        getDefault().getLog().log(new Status(IStatus.ERROR, getBundleId(), message, t));
    }
    
    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        instance = this;
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        super.stop(context);
        instance = null;
    }   
    
    public Injector getInjector(String languageName) {
        try {
            return injectors.get(languageName);
        } catch(ExecutionException e) {
            String message = "Failed to create injector for " + languageName;
            logError(message, e);
            throw new RuntimeException(message, e);
        }
    }
    
    protected Module getUiModule(String grammar) {
        if (INJECTOR_KEY.equals(grammar)) {
            return new SdkUiModule(this);
        }
        throw new IllegalArgumentException(grammar);
    }
}
